<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserprofile } from "@/api/user";
import dayjs from 'dayjs'
export default {
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value),
    };
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: "保存中...",
        forbidClick: true, // 禁止背景点击
        duration: 0, // 持续展示
      });
      //将currentDate 修改为后端需要的格式
      const currentDate = dayjs(this.currentDate).format("YYYY-MM-DD");
      try {
        await updateUserprofile({
          birthday: currentDate,
        });
        this.$toast('更改生日成功')
        this.$emit('input',currentDate)
        this.$emit('close')
      } catch (error) {
        this.$toast("修改生日日期失败,请重试");
        console.log(error);
      }
    },
  },
};
</script>

<style scoped lang="less"></style>